using System;
using System.Collections.Generic;
using com.cruxomatic.exceptions;
using com.cruxomatic.logging;
using com.cruxomatic.logging.Enums;
using com.cruxomatic.views.Enums;
using com.cruxomatic.views;
using System.Linq;
using com.cruxomatic.BusinessFacades;
using com.cruxomatic.views;
using com.cruxomatic.DTOs;


namespace com.cruxomatic.controllers.Administration
{
    public class MasterLocationController
    {
        private IMasterLocation _view;
        private MasterLocationFacade facade;

        public MasterLocationController(IMasterLocation view)
        {
             _view = view;
        }

        public void InitView(bool isPostBack)
        {
            if (!isPostBack)
            {
                OpenList();
                                        
            }
        }

        public void OpenDetail(int MasterLocationID)
        {
            facade = new MasterLocationFacade();
            _view.ErrorDetails = null;
			
	        _view.CurrentMasterLocation = facade.GetSingleMasterLocation(MasterLocationID);
            _view.FormMode = FormMode.AddEdit;
        }

        public void OpenList()
        {
            facade = new  MasterLocationFacade ();
            _view.ErrorDetails = null;
            _view.AllMasterLocation = facade.GetAllMasterLocation();
            _view.FormMode = FormMode.List;
        }


        public void SaveData()
        {
           facade = new  MasterLocationFacade ();
            try
            {
                facade.SaveMasterLocation(_view.CurrentMasterLocation);
                OpenList();
            }
            catch (CruxDatabaseException ex)
            {
                ApplicationLogger.LogApplicationMessage(
                    ex.Message, ApplicationLogType.Database, ex.StackTrace);
                _view.ErrorDetails = CruxApplicationErrorManager.GetApplicationError(ex.ErrorNameOrCode, ex.Message);
            }
            catch (Exception ex)
            {
                ApplicationLogger.LogApplicationMessage(
                    ex.Message, ApplicationLogType.Database, ex.StackTrace);
                _view.ErrorDetails = CruxApplicationErrorManager.GetApplicationError("101", ex.Message);
            }
            
        }

        public void DeleteMasterLocation(int MasterLocationID)
        {
            facade = new  MasterLocationFacade (); 
            try
            {
                facade.DeleteMasterLocation(   MasterLocationID );
                OpenList();
                
            }
            catch (CruxDatabaseException ex)
            {
                ApplicationLogger.LogApplicationMessage(
                    ex.Message, ApplicationLogType.Database, ex.StackTrace);
                _view.ErrorDetails = CruxApplicationErrorManager.GetApplicationError(ex.ErrorNameOrCode, ex.Message);
            }
            catch (Exception ex)
            {
                ApplicationLogger.LogApplicationMessage(
                    ex.Message, ApplicationLogType.Database, ex.StackTrace);
                _view.ErrorDetails = CruxApplicationErrorManager.GetApplicationError("101", ex.Message);
            }   
        }
		
		public void RedirectToPreviousPage()
		{
            NavigationController.OpenView("Administration");
		}
    }
}